/* Endless Code Copyright (C) 2009 Jingul,Kim
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
package ec.gfx.ui;

import java.util.LinkedList;

import ec.Result;
import ec.core.event.MouseEvt;
import ec.gfx.GL;
import ec.gfx.Scene;

/**
 * 여러 파트를 포함할 수 있는 파트입니다. 모든 창 형태의 인터페이스의 부모 클래스 입니다.
 * 
 * @since 2009
 * @version 1.0
 * @author <a href="mailto:jingul.kim@gmail.com">Jingul,Kim</a>
 */
public class Pack extends Part {
	private Addition additon[] = new Addition[2];
	
	private LinkedList<Part> parts = new LinkedList<Part>();

	private float pre_x, pre_y;

	private Header header = new Header();

	private Border border = new Border(header);

	public Pack() {
		setSize(30f, 20f);

		add(header);
		add(border);
	}

	public void add(Part part) {
		parts.add(part);

		part.parent = this;
		part.addNotify();
	}

	public void remove(Part part) {
		parts.remove(part);

		part.parent = null;
		part.removeNotify();
	}

	@Override
	public Result pre(Scene scene) {
		super.pre(scene);

		GL.enable(GL.GL_STENCIL_TEST);
		
		GL.clear(GL.GL_STENCIL_BUFFER_BIT);
		GL.stencilFunc(GL.GL_NEVER, 0x0, 0x0);
		GL.stencilOp(GL.GL_INCR, GL.GL_INCR, GL.GL_INCR);

		GL.rectf(0.0f, 0.0f, w, - h);
		// draw addition?;
		
		GL.stencilFunc(GL.GL_EQUAL, 0x1, 0x1);              // 0x1인 부분에 대해서만 드로잉을 허용한다.
		GL.stencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP);
		

		return Result.OK;
	}

	@Override
	public Result now(Scene scene) {
		GL.begin(GL.GL_POINTS);
		GL.vertex2f(0, 0);
		GL.end();

		GL.color3f(1.0f, 1.0f, 1.0f);
		GL.rectf(0.0f, 0.0f, w, -h);
		return super.now(scene);
	}
	
	@Override
	public Result post(Scene scene) {
		GL.disable(GL.GL_STENCIL_TEST);
		
		return super.post(scene);
	}

	@Override
	public Result render(Scene scene) {
		for (Part f : parts) {
			if (f.pre(scene) == Result.OK) {
				f.now(scene);
				f.render(scene);
			}
			f.post(scene);
		}

		return Result.OK;
	}

	@Override
	public Result update(Scene scene) {
		for (Part f : parts) {
			f.update(scene);
		}

		return Result.OK;
	}

	@Override
	public void processMouseEvt(MouseEvt e) {
		switch (e.getType()) {
		case MouseEvt.DRAG:// move
			m_matrix.add(12, e.getX() - pre_x);
			m_matrix.add(13, e.getY() - pre_y);

			pre_x = e.getX();
			pre_y = e.getY();
			break;
		case MouseEvt.DOWN:// down
			pre_x = e.getX();
			pre_y = e.getY();
			break;
		default:
		}
	}

	@Override
	public void setSize(float w, float h) {
		super.setSize(w, h);

		for (Part p : parts) {
			p.notifyResized();
		}
	}

	@Override
	public void setLocation(float x, float y) {
		super.setLocation(x, y);
	}

	public void setTitle(String text) {
		header.setText(text);
	}
}
